package com.aabte.auth.role.infrastructure.messaging.producer;

import com.aabte.auth.role.application.RoleEventPublisher;
import com.aabte.auth.role.domain.event.RoleCreated;
import com.aabte.auth.role.domain.model.Role;
import com.aabte.auth.tracker.model.Status;
import com.aabte.auth.tracker.model.Tracker;
import com.aabte.auth.tracker.repository.TrackerRepository;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.Instant;

@Component
public class RoleEventProducerImpl implements RoleEventPublisher {

    @Resource
    private ApplicationContext context;

    @Resource
    private TrackerRepository trackerRepository;

    @Override
    public void roleCreated(Role role) {
        RoleCreated roleCreated = new RoleCreated(role.getRoleId(), role.getRoleName());
        context.publishEvent(roleCreated);

        Tracker tracker = new Tracker(trackerRepository.nextTrackerId(), roleCreated.identity(), Instant.now(),
                null, 0, Status.PROCESSING);
        trackerRepository.store(tracker);
    }

}
